package cn.test.disruptor;

import com.lmax.disruptor.RingBuffer;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class OrderEventProducer {
    private RingBuffer<OrderEvent> ringBuffer;

    public OrderEventProducer(RingBuffer<OrderEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(long value, String name) {
        // 获取队列下一个槽
        long sequence = ringBuffer.next();

        try {
            // 获取消息
            OrderEvent orderEvent = ringBuffer.get(sequence);

            // 写入消息数据
            orderEvent.setValue(value);
            orderEvent.setName(name);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.debug("生产者发送数据value：{}，name：{}", value, name);
            // 发布事件
            ringBuffer.publish(sequence);
        }
    }
}
